students = [
    {'name': 'A', 'age': 17, 'id': '000238', 'gender': 'f', 'score': 97},
    {'name': 'B', 'age': 20, 'id': '030236', 'gender': 'u', 'score': 48},
    {'name': 'C', 'age': 14, 'id': '000438', 'gender': 'm', 'score': 98},
    {'name': 'D', 'age': 16, 'id': '000234', 'gender': 'm', 'score': 87},
    {'name': 'E', 'age': 21, 'id': '000239', 'gender': 'm', 'score': 57},
    {'name': 'F', 'age': 17, 'id': '000230', 'gender': 'f', 'score': 98}]


# Question1
# 删除性别不明的student
# method1: 创建新list
new_student = [student for student in students if student['gender'] != 'u']
print(new_student)
# method2: while
i = 0
while i < len(students):
    if students[i]['gender'] == 'u':
        students.remove(students[i])
        continue  # index==i 的记录删除后，students[i]为未遍历到的数据，使用continue防止漏删
    i += 1
print(students)
students = temp_students
# method3: 使用list切片[::]即[:]
for student in students[:]:
    if student['gender'] == 'u':
        students.remove(student)
print(students)
students = temp_students
# method4: 使用for倒着删
#  1, 2, 3, 4, 5
#  某轮循环删除2后迭代器指向3，再迭代时迭代器指向4，因此3就被跳过了，所以要倒着删
i = 0
for i in range(len(students) - 1, -1, -1):
    if students[i]['gender'] == 'u':
        students.remove(student[i])
print(students)
students = temp_students

# Question2
# 统计不及格人数，并且打印姓名、分数
count = 0
for student in students:
    if student['score'] < 60:
        count += 1
        print('%s不及格，考了%d' % (student['name'], student['score']))
print('共有%d人不及格' % count)

# Question3
# 打印id尾号为8的id
for student in students:
    if student['id'].endswith('8'):
        print(student['id'])

# Question4
# 统计最高分人数，打印最高分同学姓名
num_max = 0
max_score = 0
for student in students:
    if student['score'] > max_score:
        max_score = student['score']
print('max_score = %d' % max_score)
for student in students:
    if student['score'] == max_score:
        print(student['name'])
